package com.xn.app.muying.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.GridView;
import android.widget.ListView;

import com.xn.app.muying.R;
import com.xn.app.muying.adpter.HomeXuanChuanListViewAdaper;
import com.xn.app.muying.bean.BeanList;
import com.xn.app.muying.bean.HomeXuanChuanBean;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/16.
 */
public class XuanChuanActivity extends AppCompatActivity implements HttpUtil.OnGetDataListener {

    private ListView listView;
    private GridView gridView;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_xuanchuan);

        listView = (ListView) findViewById(R.id.xuanchuan_listView);
        gridView = (GridView) findViewById(R.id.xuanchuan_gridView);

        ArrayList<String> shuju = new ArrayList();
        int a = 0;
        for (; a < 6; a++) {
            shuju.add(a + "");
        }

        HttpUtil.getData(APIMethod.Home_XuanChuan, handler, this);
        HomeXuanChuanListViewAdaper homeAdapter = new HomeXuanChuanListViewAdaper(this, shuju);
        listView.setAdapter(homeAdapter);

    }

    @Override
    public void onJsonData(String data) {
        Log.i("66666", data.toString());
        ArrayList<BeanList> list = new ArrayList<>();
        BeanList beanList = new BeanList();
        ArrayList<HomeXuanChuanBean> list1 = new ArrayList<>();
        ArrayList<HomeXuanChuanBean> list2 = new ArrayList<>();
        ArrayList<HomeXuanChuanBean> list3 = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name1 = jsonObject.optString("name");
                HomeXuanChuanBean homeXuanChuanBean1 = new HomeXuanChuanBean();
                homeXuanChuanBean1.setName1(name1);
                list1.add(homeXuanChuanBean1);
                beanList.setLsit(list1);
                list.add(beanList);
                Log.i("77777",list.toString());
                JSONArray subCategories = jsonObject.getJSONArray("subCategories");
                if (subCategories.length() > 0) {
                    for (int j = 0; j < subCategories.length(); j++) {
                        JSONObject jsonObject1 = subCategories.getJSONObject(j);
                        String name2 = jsonObject1.optString("name");
                        HomeXuanChuanBean homeXuanChuanBean2 = new HomeXuanChuanBean();
                        homeXuanChuanBean2.setName2(name2);
                        list2.add(homeXuanChuanBean2);
                        beanList.setLsit1(list2);
                        list.add(beanList);
                        Log.i("88888",list.toString());
                        JSONArray subCategories1 = jsonObject1.getJSONArray("subCategories");
                        if (subCategories1.length() > 0) {
                            for (int k = 0; k < subCategories1.length(); k++) {
                                JSONObject jsonObject2 = subCategories1.getJSONObject(k);
                                String name3 = jsonObject2.optString("name");
                                HomeXuanChuanBean homeXuanChuanBean3 = new HomeXuanChuanBean();
                                homeXuanChuanBean3.setName3(name3);
                                list3.add(homeXuanChuanBean3);
                                beanList.setLsit2(list3);
                                list.add(beanList);
                                Log.i("99999",list.toString());
                            }


                        }

                    }

                }

            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
